package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Item;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import util.KeyGen;
import util.ParameterCheck;
import dao.ItemDao;

/**
 * Servlet implementation class ShowItem
 */
public class ShowItem extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(ShowItem.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String msg = "";
		String result = "";
		String iid = request.getParameter("itemid");
		Item item = null;
		if (ParameterCheck.isEmpty(iid)) {
			msg = "parameter is illegal";

		} else {
			//			iid = KeyGen.getId(iid);
			int itemid = KeyGen.convertCode2Id(iid);
			ItemDao id = new ItemDao();
			item = id.query(itemid);
			msg = "ok";
			result = JSONObject.fromObject(item).toString();
		}
		log.info(msg);
		log.info(result);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msg", msg);
		jsonObject.put("res", result);
		
		out.print(jsonObject.toString());

		out.flush();
		out.close();
	}
}
